package com.gps.cms.dao.impl;

import java.util.List;

import com.gps.cms.dao.IUserDAO;
import com.gps.cms.model.User;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.annotation.Resource;
import oracle.jdbc.OracleTypes;
import org.hibernate.SessionFactory;

import org.hibernate.jdbc.Work;

/**
 * 
 * User DAO
 * 
 * @author nameco
 * @version 1.0.0
 *
 */

public class UserDAOImpl implements IUserDAO {
	
         List<User> result = new ArrayList<User>();
	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 * 
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 * 
	 * @param SessionFactory - Hibernate Session Factory
	 */
        @Resource(name = "FirtSessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

	/**
	 * Add User
	 * 
	 * @param  User user
	 */
	
    @Override
	public void addUser(User user) {
		getSessionFactory().getCurrentSession().save(user);
	}

	/**
	 * Delete User
	 * 
	 * @param  User user
	 */
	
    @Override
	public void deleteUser(User user) {
		getSessionFactory().getCurrentSession().delete(user);
	}

	/**
	 * Update User
	 * 
	 * @param  User user
	 */

    @Override
	public void updateUser(User user) {
		getSessionFactory().getCurrentSession().update(user);
	}

	/**
	 * Get User
	 * 
	 * @param  int User Id
	 * @return User 
	 */

    @Override
	public User getUserById(int id) {
		List list = getSessionFactory().getCurrentSession().createQuery("from S_USERS where id=?").setParameter(0, id).list();
		return (User)list.get(0);
	}

	/**
	 * Get User List
	 * 
	 * @return List - User list
	 */
    @Override
	public List<User> getUsers() {
		List list = getSessionFactory().getCurrentSession().createSQLQuery("SELECT * FROM S_Users").addEntity(User.class).list();
		return list;
	}

    @Override
    public List<User> getListUser() {
        
           getSessionFactory().getCurrentSession().doWork(new Work() {

            @Override
            public void execute(Connection conn) throws SQLException {
                CallableStatement st = conn.prepareCall("{ call S_USERS_GETALL(?) }");
                st.registerOutParameter(1, OracleTypes.CURSOR);
                st.execute();
                ResultSet rs = (ResultSet) st.getObject(1);
                while (rs.next()) {
                    result.add(mapUser(rs));
                }
            }
        });
     
        return result;
        
    }

    
      private static User mapUser(ResultSet resultSet) throws SQLException {
        return new User(
                resultSet.getInt("id"),
                resultSet.getString("username"),
                resultSet.getString("password"));
    }
}
